package com.fengke.service.impl;

import java.util.ArrayList;
import java.util.List;

import org.apache.log4j.Logger;

import com.fengke.dao.model.TagModel;
import com.fengke.dao.tag.TagManager;
import com.fengke.service.TagService;
import com.fengke.service.vo.TagVO;
import com.fengke.util.LoggerFactory;

public class TagServiceImpl implements TagService {
	private Logger logger = LoggerFactory.getLogger(TagServiceImpl.class);
	private TagManager tagManager;
	@Override
	public List<TagVO> researchTagVOList(TagVO tagVO) throws Exception {
		if(tagVO.getTagModel() == null) {
			tagVO.setTagModel(new TagModel());
		}
		List<TagModel> tagModelList = null;
		try {
			tagModelList = tagManager.researchTagModelList(tagVO.getTagModel());
		} catch (Exception e) {
			logger.error("researchTagModelList Exception! tagVO=" + tagVO, e);
		}
		if(tagModelList != null && tagModelList.size() > 0) {
			List<TagVO> voList = new ArrayList<TagVO>();
			for(int i = 0; i < tagModelList.size(); i ++) {
				if(tagModelList.get(i) != null) {
					TagVO vo = new TagVO();
					vo.setTagModel(tagModelList.get(i));
					voList.add(vo);
				}
			}
			return voList;
		}
		return null;
	}

	@Override
	public TagVO createTag(TagVO tagVO) throws Exception {
		if(tagVO == null || tagVO.getTagModel() == null) {
			return null;
		}
		try {
			tagVO.getTagModel().setId(tagManager.createTag(tagVO.getTagModel()));
		} catch (Exception e) {
			logger.error("createTag Exception! tagVO=" + tagVO, e);
		}
		return tagVO;
	}

	@Override
	public TagVO updateTag(TagVO tagVO) throws Exception {
		try {
			tagManager.updateTagById(tagVO.getTagModel());
		} catch (Exception e) {
			logger.error("updateTagById Exception! tagVO=" + tagVO, e);
		}
		return tagVO;
	}

	@Override
	public int deleteTagByIds(List<String> list) throws Exception {
		int count = 0;
		try {
			count = tagManager.deleteTagByIds(list);
		} catch (Exception e) {
			logger.error("deleteTagByIds Exception! list=" + list, e);
		}
		return count;
	}

	public TagManager getTagManager() {
		return tagManager;
	}

	public void setTagManager(TagManager tagManager) {
		this.tagManager = tagManager;
	}

}
